package com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibleutil;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import androidx.annotation.IntDef;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class FundibleASFileUtil {

    public String crGW="";
    public String m8fc="";
    public String ci5u="";
    public String xBnr="";
    public String a5b3="";
    public String b9NF="";
    public String ypBA="";

    public static File getFileByPath_fundible(final String filePath) {
        return TextUtils.isEmpty(filePath) ? null : new File(filePath);
    }

    public static boolean isFileExists_fundible(Context context, final String filePath) {
        File file = getFileByPath_fundible(filePath);
        if (file == null) return false;
        if (file.exists()) {
            return true;
        }
        return isFileExistsApi29_fundible(context, filePath);
    }

    private static boolean isFileExistsApi29_fundible(Context context, String filePath) {
        if (Build.VERSION.SDK_INT >= 29) {
            try {
                Uri uri = Uri.parse(filePath);
                ContentResolver cr = context.getContentResolver();
                AssetFileDescriptor afd = cr.openAssetFileDescriptor(uri, "r");
                if (afd == null) return false;
                try {
                    afd.close();
                } catch (IOException ignore) {
                }
            } catch (FileNotFoundException e) {
                return false;
            }
            return true;
        }
        return false;
    }

    public static long getFileLastModified_fundible(final File file) {
        if (file == null) return -1;
        return file.lastModified();
    }

    public static long getFileLastModified_fundible(final String filePath) {
        return getFileLastModified_fundible(getFileByPath_fundible(filePath));
    }

    public static boolean isDir_fundible(final String dirPath) {
        return isDir_fundible(getFileByPath_fundible(dirPath));
    }

    public static boolean isDir_fundible(final File file) {
        return file != null && file.exists() && file.isDirectory();
    }

    public static String getFileName_fundible(final File file) {
        if (file == null) return "";
        return getFileName_fundible(file.getAbsolutePath());
    }

    public static String getFileName_fundible(final String filePath) {
        if (TextUtils.isEmpty(filePath)) return "";
        int lastSep = filePath.lastIndexOf(File.separator);
        return lastSep == -1 ? filePath : filePath.substring(lastSep + 1);
    }

    public static String getSize_fundible(final String filePath) {
        return getSize_fundible(getFileByPath_fundible(filePath));
    }

    public static String getSize_fundible(final File file) {
        if (file == null) return "";
        if (file.isDirectory()) {
            return getDirSize_fundible(file);
        }
        return getFileSize_fundible(file);
    }

    private static String getDirSize_fundible(final File dir) {
        long len = getDirLength_fundible(dir);
        return len == -1 ? "" : byte2FitMemorySize_fundible(len);
    }

    private static String getFileSize_fundible(final File file) {
        long len = getFileLength_fundible(file);
        return len == -1 ? "" : byte2FitMemorySize_fundible(len);
    }

    public static long getLength_fundible(final String filePath) {
        return getLength_fundible(getFileByPath_fundible(filePath));
    }

    public static long getLength_fundible(final File file) {
        if (file == null) return 0;
        if (file.isDirectory()) {
            return getDirLength_fundible(file);
        }
        return getFileLength_fundible(file);
    }

    private static long getDirLength_fundible(final File dir) {
        if (!isDir_fundible(dir)) return 0;
        long len = 0;
        File[] files = dir.listFiles();
        if (files != null && files.length > 0) {
            for (File file : files) {
                if (file.isDirectory()) {
                    len += getDirLength_fundible(file);
                } else {
                    len += file.length();
                }
            }
        }
        return len;
    }

    public static long getFileLength_fundible(final String filePath) {
        boolean isURL = filePath.matches("[a-zA-z]+://[^\\s]*");
        if (isURL) {
            try {
                HttpsURLConnection conn = (HttpsURLConnection) new URL(filePath).openConnection();
                conn.setRequestProperty("Accept-Encoding", "identity");
                conn.connect();
                if (conn.getResponseCode() == 200) {
                    return conn.getContentLength();
                }
                return -1;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return getFileLength_fundible(getFileByPath_fundible(filePath));
    }

    private static long getFileLength_fundible(final File file) {
        if (!isFile_fundible(file)) return -1;
        return file.length();
    }

    public static boolean isFile_fundible(final String filePath) {
        return isFile_fundible(getFileByPath_fundible(filePath));
    }

    public static boolean isFile_fundible(final File file) {
        return file != null && file.exists() && file.isFile();
    }

    @SuppressLint("DefaultLocale")
    public static String byte2FitMemorySize_fundible(final long byteSize) {
        int precision = 3;
        if (byteSize < 0) {
            throw new IllegalArgumentException("byteSize shouldn't be less than zero!");
        } else if (byteSize < MemoryConstants_fundible.KB) {
            return String.format("%." + precision + "fB", (double) byteSize);
        } else if (byteSize < MemoryConstants_fundible.MB) {
            return String.format("%." + precision + "fKB", (double) byteSize / MemoryConstants_fundible.KB);
        } else if (byteSize < MemoryConstants_fundible.GB) {
            return String.format("%." + precision + "fMB", (double) byteSize / MemoryConstants_fundible.MB);
        } else {
            return String.format("%." + precision + "fGB", (double) byteSize / MemoryConstants_fundible.GB);
        }
    }

    public static final class MemoryConstants_fundible {
        public static final int BYTE = 1;
        public static final int KB   = 1024;
        public static final int MB   = 1048576;
        public static final int GB   = 1073741824;

        @IntDef({BYTE, KB, MB, GB})
        @Retention(RetentionPolicy.SOURCE)
        public @interface Unit {
        }
    }

    public void setCrGW(String crGW) {
        this.crGW = crGW;
    }

    public void setM8fc(String m8fc) {
        this.m8fc = m8fc;
    }

    public void setCi5u(String ci5u) {
        this.ci5u = ci5u;
    }

    public void setxBnr(String xBnr) {
        this.xBnr = xBnr;
    }

    public void setA5b3(String a5b3) {
        this.a5b3 = a5b3;
    }

    public void setB9NF(String b9NF) {
        this.b9NF = b9NF;
    }

    public void setYpBA(String ypBA) {
        this.ypBA = ypBA;
    }

    public String getCrGW() {
        return crGW;
    }

    public String getM8fc() {
        return m8fc;
    }

    public String getCi5u() {
        return ci5u;
    }

    public String getxBnr() {
        return xBnr;
    }

    public String getA5b3() {
        return a5b3;
    }

    public String getB9NF() {
        return b9NF;
    }

    public String getYpBA() {
        return ypBA;
    }
}
